package de.lmu.ifi.pst.uwejsf.framework;

import java.util.HashMap;
import java.util.Map;

/**
 * This is the abstract base class for all navigation node classes. It mainly stores content selectors and guards.
 * 
 * @author Christian
 *
 */
public abstract class NavigationNodeBean {

	protected NavigationPropertyResolver navigationPropertyResolver;

	public NavigationPropertyResolver getNavigationPropertyResolver() {
		return navigationPropertyResolver;
	}

	public void setNavigationPropertyResolver(NavigationPropertyResolver npr) {
		navigationPropertyResolver = npr;
	}

	protected ContentSelector defaultContentSelector;

	public ContentSelector getDefaultContentSelector() {
		return defaultContentSelector;
	}

	protected ContentSelector activeContentSelector = null;

	public ContentSelector getActiveContentSelector() {
		return activeContentSelector;
	}

	public void setActiveContentSelector(ContentSelector cs) {
		activeContentSelector = cs;
	}

	protected Map<String, ContentSelector> contentSelectors = new HashMap<String, ContentSelector>();

	public Map<String, ContentSelector> getContentSelectors() {
		return contentSelectors;
	}

	public void setContentSelectors(Map<String, ContentSelector> cs) {
		this.contentSelectors = cs;
	}

	protected Map<String, GuardHandler> guards = new HashMap<String, GuardHandler>();

	/**
	 * Return the node's map of guard handlers for all content selectors. 
	 * The content selector's name is used as a key. 
	 *
	 */
	public Map<String, GuardHandler> getGuards() {
		return guards;
	}

	public void setGuards(Map<String, GuardHandler> guards) {
		this.guards = guards;
	}

	public AbstractUWENavigator getUWENavigator() {
		return (AbstractUWENavigator) ExpressionHelpers
				.resolveValue("#{uweNavigator}");
	}

	protected GuardHandler nodeGuard;

	/**
	 * Return the GuardHandler of the node's guard. This guard is evaluated every time the node becomes active.
	 */
	public GuardHandler getNodeGuard() {
		return nodeGuard;
	}

	/**
	 * Set the GuardHandler of the node's guard. 
	 * @param gh
	 */
	public void setNodeGuard(GuardHandler gh) {
		nodeGuard = gh;
	}

	/**
	 * Check the guard conditions of the node and of the active content
	 * selector
	 * 
	 * @return true if no guard condition is violated
	 */
	public boolean checkCurrentGuards() {
		AbstractUWEContextFactory contextFactory = AbstractUWEContextFactory
				.getCurrentInstance();
		boolean check = true;
		if (nodeGuard != null) {
			check = nodeGuard.checkCondition(contextFactory);
			if (check == false)
				return false;
		}
		if (activeContentSelector != null && activeContentSelector.getName() != null) {
			
			GuardHandler gh = guards.get(activeContentSelector.getName());
			if (gh != null)
				check = gh.checkCondition(contextFactory);
		}
		return check;

	}

}
